package com.ruoyi.im.task;

import com.ruoyi.im.controller.ServerEndPoint;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
@EnableScheduling
public class SessionOfflineChkTask {


    /**
     * 每分钟检查一下当前服务的会话
     */
    @Scheduled(cron = "0 1/1 * * * ?")
    public void chk() {
        List<Integer> offlineUserList = new ArrayList<>();
        Map<Integer, Session> onlineSessionList = ServerEndPoint.onlineSessionList;
        onlineSessionList.forEach((key, val) -> {
            if (!val.isOpen()) {
                //清空
                offlineUserList.add(key);
            }
        });
        for (Integer integer : offlineUserList) {
            ServerEndPoint.onlineSessionList.remove(integer);
        }
    }


}
